/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Correo;

import java.util.Vector;

/**
 *
 * @author estudiante
 */
public class ZDespachadorMail implements Runnable {

    Thread hiloDespachador;
    boolean sw = true;
    MailSMTP smtp = new MailSMTP("virtual.fcet.uagrm.edu.bo", 25, "grupo12sa@virtual.fcet.uagrm.edu.bo");
    public static Vector<Mail> vectorMails = new Vector<Mail>();

    public ZDespachadorMail() {
    }

    public void iniciarHilo() {
        hiloDespachador = new Thread(this);
        hiloDespachador.start();
    }

    public ZDespachadorMail(ZConfiguracion c) {
        smtp = new MailSMTP(c.getDominio(), c.getPuertoEnvio(), c.getUsuario());
    }

    public void run() {
        while (sw) {
            try {
                if (vectorMails.size() > 0) {
                    if (smtp.conectar()) {
                        Mail m = vectorMails.remove(0);
                        smtp.setData(m.getData());
                        smtp.setOrigen(m.getOrigen());
                        smtp.setDestino(m.getDestino());
                        smtp.setSubject(m.getSubject());
                        smtp.EnviarMail();
                        System.out.println("Smtp ha enviado " + m.toString());

                        smtp.deconectar();
                    }
                }
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
                sw = false;
            }
        }
    }    
}
